/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller.evaluation.document;

import cn.ac.nercis.pes.common.constant.project.GenerateDocument;
import cn.ac.nercis.pes.community.component.controls.MenuListCell;
import cn.ac.nercis.pes.community.model.vo.MenuVO;
import cn.ac.nercis.pes.community.utils.AppStore;
import cn.ac.nercis.pes.community.utils.LogUtils;
import cn.ac.nercis.pes.community.views.evaluation.document.ReportDocView;
import cn.ac.nercis.pes.community.views.evaluation.document.SchemeDocView;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;

/**
 * 生成文档控制器
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public class GenerateDocController implements Initializable {

    @FXML
    private StackPane page;

    @FXML
    private BorderPane generateDocPane;
    @FXML
    private ListView<MenuVO> menuListView;

    private GenerateDocument currentDocument;
    private String currentSystemId;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initializeMenu();
    }

    public void initializeData(String systemId){
        LogUtils.info(log, "初始化生成文档...");
        this.currentSystemId = systemId;
        if(Objects.isNull(currentDocument)){
            menuListView.getSelectionModel().select(0);
        }else{
            loadContent();
        }
    }

    public StackPane getPage(){
        return this.page;
    }

    private void initializeMenu(){
        menuListView.setCellFactory(listView -> new MenuListCell());
        menuListView.getSelectionModel()
                .selectedItemProperty()
                .addListener((observable,oldValue,newValue)->{
                    if(Objects.nonNull(newValue)){
                        currentDocument = GenerateDocument.fromName(newValue.getTitle());
                        loadContent();
                    }
                });

        for(GenerateDocument menu : GenerateDocument.findSystemDocuments()){
            menuListView.getItems().add(new MenuVO(1,"",menu.getName(),"generate-report-icon"));
        }

    }

    private void loadContent(){
        switch (currentDocument) {
            case SCHEME_DOC -> {
                var schemeView = AppStore.getEvaluationView(this.currentSystemId, SchemeDocView.class);
                generateDocPane.setCenter(Objects.requireNonNull(schemeView).load());
                if (schemeView.getController() instanceof SchemeDocController controller) {
                    controller.initializeData(this.currentSystemId);
                }
            }
            case REPORT_DOC -> {
                var reportView = AppStore.getEvaluationView(this.currentSystemId, ReportDocView.class);
                generateDocPane.setCenter(Objects.requireNonNull(reportView).load());
                if (reportView.getController() instanceof ReportDocController controller) {
                    controller.initializeData(this.currentSystemId);
                }
            }
        }
    }
}